<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TCP四次挥手详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
        }
        .step-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            position: relative;
            z-index: 1;
        }
        .highlight:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 40%;
            background-color: rgba(74, 144, 226, 0.2);
            z-index: -1;
            transform: skewY(-1deg);
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 4rem;
            line-height: 0.8;
            margin-right: 0.5rem;
            font-weight: 700;
            color: #1e3c72;
        }
        .mermaid {
            background: white;
            border-radius: 0.5rem;
            padding: 1.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 md:py-32 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">TCP四次挥手详解</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8 max-w-3xl mx-auto leading-relaxed">
                深入解析TCP连接终止机制，理解数据传输完整性保障的关键步骤
            </p>
            <div class="flex justify-center space-x-4">
                <a href="#process" class="px-6 py-3 bg-white text-blue-900 font-medium rounded-full hover:bg-gray-100 transition duration-300 transform hover:-translate-y-1">
                    <i class="fas fa-project-diagram mr-2"></i>查看流程图
                </a>
                <a href="#details" class="px-6 py-3 border-2 border-white text-white font-medium rounded-full hover:bg-white hover:text-blue-900 transition duration-300 transform hover:-translate-y-1">
                    <i class="fas fa-book-open mr-2"></i>阅读详情
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl px-4 py-12 md:py-20">
        <!-- Intro Section -->
        <section class="mb-16">
            <div class="drop-cap text-lg md:text-xl text-gray-700 leading-relaxed">
                TCP的四次挥手过程通过四个步骤确保连接的可靠关闭。首先，主动关闭方发送FIN包请求关闭连接；其次，被动关闭方确认收到关闭请求并发送自己的FIN包；然后，主动关闭方确认收到被动关闭方的FIN包；最后，双方在TIME-WAIT状态下等待足够的时间以确保所有数据包都被正确处理。这个过程确保了数据传输的完整性和资源的正确释放。
            </div>
        </section>

        <!-- Background Section -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-6 text-blue-900 flex items-center">
                <span class="w-8 h-8 bg-blue-900 text-white rounded-full flex items-center justify-center mr-4">1</span>
                <span class="highlight">四次挥手的背景</span>
            </h2>
            <div class="bg-gray-50 p-6 rounded-xl border-l-4 border-blue-500">
                <p class="text-gray-700 mb-4">TCP是一种面向连接的协议，确保数据在发送和接收过程中按顺序到达并完整传输。在数据传输完成后，双方需要进行连接终止，以释放占用的资源。</p>
                <p class="text-gray-700">这时，四次挥手过程发挥了作用，确保双方都能正确关闭连接。</p>
            </div>
        </section>

        <!-- Process Section -->
        <section class="mb-20" id="process">
            <h2 class="text-3xl font-bold mb-10 text-blue-900 flex items-center">
                <span class="w-8 h-8 bg-blue-900 text-white rounded-full flex items-center justify-center mr-4">2</span>
                <span class="highlight">四次挥手的详细过程</span>
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Step 1 -->
                <div class="step-card bg-white rounded-xl shadow-md p-6 border-t-4 border-blue-500 transition duration-300">
                    <div class="flex items-center mb-4">
                        <div class="bg-blue-100 text-blue-800 w-10 h-10 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-paper-plane"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">第一次挥手</h3>
                    </div>
                    <p class="text-gray-700 mb-2"><strong>FIN</strong>：主动关闭方（例如客户端）发送一个FIN（Finish）包给被动关闭方（例如服务器），表示它的数据传输完毕，要求终止连接。</p>
                    <p class="text-gray-700 mb-4">这个包的SYN标志被置为1，同时ACK标志也被置为1。</p>
                    <div class="bg-blue-50 text-blue-800 px-3 py-2 rounded text-sm">
                        <i class="fas fa-info-circle mr-2"></i>状态：主动关闭方进入FIN-WAIT-1状态
                    </div>
                </div>

                <!-- Step 2 -->
                <div class="step-card bg-white rounded-xl shadow-md p-6 border-t-4 border-green-500 transition duration-300">
                    <div class="flex items-center mb-4">
                        <div class="bg-green-100 text-green-800 w-10 h-10 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-check-circle"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">第二次挥手</h3>
                    </div>
                    <p class="text-gray-700 mb-2"><strong>ACK</strong>：被动关闭方接收到FIN包后，回复一个带有ACK标志的包，确认收到终止请求。</p>
                    <p class="text-gray-700 mb-4">这时，被动关闭方进入CLOSE-WAIT状态。</p>
                    <div class="bg-green-50 text-green-800 px-3 py-2 rounded text-sm">
                        <i class="fas fa-info-circle mr-2"></i>状态：被动关闭方发送ACK包后，进入LAST-ACK状态
                    </div>
                </div>

                <!-- Step 3 -->
                <div class="step-card bg-white rounded-xl shadow-md p-6 border-t-4 border-purple-500 transition duration-300">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-800 w-10 h-10 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-paper-plane"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">第三次挥手</h3>
                    </div>
                    <p class="text-gray-700 mb-2"><strong>FIN</strong>：被动关闭方在确认收到主动关闭方的FIN包后，发送自己的FIN包给主动关闭方，表示它的数据传输也已完毕，请求终止连接。</p>
                    <div class="bg-purple-50 text-purple-800 px-3 py-2 rounded text-sm">
                        <i class="fas fa-info-circle mr-2"></i>状态：被动关闭方进入TIME-WAIT状态
                    </div>
                </div>

                <!-- Step 4 -->
                <div class="step-card bg-white rounded-xl shadow-md p-6 border-t-4 border-orange-500 transition duration-300">
                    <div class="flex items-center mb-4">
                        <div class="bg-orange-100 text-orange-800 w-10 h-10 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-check-circle"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">第四次挥手</h3>
                    </div>
                    <p class="text-gray-700 mb-2"><strong>ACK</strong>：主动关闭方接收到被动关闭方的FIN包后，回复一个ACK包，确认终止请求。</p>
                    <p class="text-gray-700 mb-4">主动关闭方进入TIME-WAIT状态，等待一段时间（通常为2MSL，即Maximum Segment Lifetime），确保所有的数据包都被接收和处理完毕后，最终关闭连接。</p>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-20" id="details">
            <h2 class="text-3xl font-bold mb-6 text-blue-900">流程可视化</h2>
            <div class="mermaid">
                sequenceDiagram
                    participant 主动关闭方
                    participant 被动关闭方
                    主动关闭方->>被动关闭方: FIN (FIN-WAIT-1)
                    被动关闭方-->>主动关闭方: ACK (CLOSE-WAIT)
                    被动关闭方->>主动关闭方: FIN (LAST-ACK)
                    主动关闭方-->>被动关闭方: ACK (TIME-WAIT)
            </div>
        </section>

        <!-- Why Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-blue-900 flex items-center">
                <span class="w-8 h-8 bg-blue-900 text-white rounded-full flex items-center justify-center mr-4">3</span>
                <span class="highlight">为什么需要四次挥手</span>
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-6 rounded-xl shadow-sm border-l-4 border-blue-400">
                    <div class="text-blue-500 mb-4 text-4xl">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">确保数据完整性</h3>
                    <p class="text-gray-700">在关闭连接之前，四次挥手确保双方都能确认已经完成所有数据的发送和接收，避免数据丢失。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm border-l-4 border-green-400">
                    <div class="text-green-500 mb-4 text-4xl">
                        <i class="fas fa-recycle"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">资源释放</h3>
                    <p class="text-gray-700">四次挥手过程中，双方逐步关闭连接，释放占用的资源，防止资源泄露和连接资源的过度占用。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm border-l-4 border-purple-400">
                    <div class="text-purple-500 mb-4 text-4xl">
                        <i class="fas fa-lock"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">确保可靠性</h3>
                    <p class="text-gray-700">通过四次挥手，确保双方都能正确地确认对方的关闭请求，防止连接被意外中断或不完全关闭。</p>
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col items-center">
                <h3 class="text-xl font-medium text-white mb-2">技术小馆</h3>
                <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-300" target="_blank">
                    <i class="fas fa-link mr-2"></i>www.yuque.com/jtostring
                </a>
                <div class="mt-6">
                    <a href="#" class="text-gray-400 hover:text-white mx-3 transition duration-300">
                        <i class="fab fa-github text-xl"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-white mx-3 transition duration-300">
                        <i class="fab fa-twitter text-xl"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-white mx-3 transition duration-300">
                        <i class="fab fa-weixin text-xl"></i>
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>